

package ro.ase.catalog.security.dao.hibernate;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.jmx.export.annotation.ManagedResource;
import ro.ase.catalog.beans.User;
import ro.ase.catalog.dao.hibernate.GenericDAOImpl;
import ro.ase.catalog.security.dao.IUserDAO;

import java.util.List;


@ManagedResource(objectName = "catalog:type=dao,name=UserDaoImpl", description = "catalog UserDaoImpl Bean")
public class UserDAOImpl extends GenericDAOImpl<User, Integer> implements IUserDAO {
    
    @SuppressWarnings("unchecked")
    public User findByUsername(String username) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Expression.eq("loginname", username));


        List<User> list = getHibernateTemplate().findByCriteria(criteria);
        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    public Integer findByFullnameVagueCount(String fullname) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Expression.like("fullname", "%" + fullname + "%"));
        criteria.setProjection(Projections.rowCount());
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
    }

    @SuppressWarnings("unchecked")
    public List<User> findByFullnameVaguePerPage(String fullname, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Expression.like("fullname", "%" + fullname + "%"));
        return getHibernateTemplate().findByCriteria(criteria, start, limit);
    }    
    
    /*
     *
     * criteria.setProjection(Projections.projectionList()
     *        .add(Projections.property("id"))
     *        .add(Projections.property("fullname"))
     *        .add(Projections.property("loginname"))                
     *        .add(Projections.property("emailaddress"))
     *        .add(Projections.property("enabled"))                
     * );
     * 
     *
     * getHibernateTemplate().find("select u from User u join u.roles as r where r.id=?",roleId );
     * 
     */    
    @SuppressWarnings("unchecked")
    public List<User> findByRoleIdPerPage(int roleId, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class)
                                        .createCriteria("roles","r")
                                        .add(Expression.eq("r.id",roleId));
        return getHibernateTemplate().findByCriteria(criteria,start,limit);
    }
    
    public Integer findByRoleIdCount(int roleId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.setProjection(Projections.rowCount());
        DetachedCriteria subCriteria = criteria.createCriteria("roles");
        subCriteria.add(Expression.eq("id",roleId));
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
//        List<User> list = getHibernateTemplate().find("select u from User u join u.roles as r where r.id=?",roleId );
//        return list;
    }
    
}
